var dflag="0";
var sflag="0";
var a1="";
var b1="";
var a2="";
var b2="";
var polygon="";

map.addEventListener("click", function(e){ 
	if (window.parent.document.getElementById('ckkuangxuan')!=undefined){
		if (window.parent.document.getElementById('ckkuangxuan').checked){
			dflag="1";
		}
		else{
			dflag="0";
		}
	
		if(sflag=="1"){
			dflag="0";
			clearUnSelectPoint(a1,b1,a2,b2);
			showKuangxuanguanduan(a1,b1,a2,b2);
			return;
		}	  
		if(dflag=="1"){
			a1=e.point.lng;
			b1=e.point.lat;
			beginMove();
		} 
	 
	}
	
}); 


function beginMove(){
	map.addEventListener("mousemove", function(e){ 
		if(dflag=="1"){
			if(polygon!=""){
				map.removeOverlay(polygon);
				polygon="";
			}
			a2=e.point.lng;
			b2=e.point.lat;
			polygon = new BMap.Polygon([
			  new BMap.Point(a1,b1),
			  new BMap.Point(a2,b1),
			  new BMap.Point(a2,b2),
			  new BMap.Point(a1,b2)
			], {strokeColor:"blue", strokeWeight:1, strokeOpacity:0.5});
			map.addOverlay(polygon);
			sflag="1";
			
		} 
	});	
}

function clearUnSelectPoint(a1,b1,a2,b2){
	for(var j=0;j<markers.length;j++){
		var mpoint = markers[j].getPosition();
		if(mpoint.lng>a1&&mpoint.lng>a2){
			map.removeControl(markers[j]);
		}
		else if(mpoint.lng<a1&&mpoint.lng<a2){
			map.removeControl(markers[j]);
		}
		if(mpoint.lat>b1&&mpoint.lat>b2){
			map.removeControl(markers[j]);
		}
		else if(mpoint.lat<b1&&mpoint.lat<b2){
			map.removeControl(markers[j]);
		}
	}
}
//删除框选框
function removeKuangxuan(){
	map.removeOverlay(polygon);
}

function showKuangxuanguanduan(a1,b1,a2,b2){
	var actionUrl = webroot+"/guanxianAction!getGuanduankuangxuan.action?a1="+a1+"&b1="+b1+"&a2="+a2+"&b2="+b2;  
	 		$.ajax({  
			  url : actionUrl,  
		      type : "post",   
		      dataType : "json",  
		      cache : false,  
		      error : function(textStatus, errorThrown) {  
		          alert("系统ajax交互错误: " + textStatus.value);  
		      },  
		      success : function(data, textStatus) {
		      	//把管段信息显示到页面上
		      	removeGuanduanAll();
		      	showGuanduanlines(data.guanduandata);
		      	var obj = new Object();
		      	obj.a1 = a1;
		      	obj.b1 = b1;
		      	obj.a2 = a2;
		      	obj.b2 = b2;
		      	//刷新左侧数信息
		      	window.parent.refreshtreebykey('kuangxuan',obj);
		    }  
	});
}
